package com.feicheng.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.feicheng.common.constant.Constant;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.message.LoginMessage;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.common.util.StringGenerator;
import com.feicheng.common.wx.IWxInfo;
import com.feicheng.common.wx.WxHTTPInterface;
import com.feicheng.persistence.entity.*;
import com.feicheng.persistence.service.*;
import com.feicheng.service.IFLoginService;
import com.feicheng.wxapi.app.WxApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service("wx-app-code-login")
public class FWxAppCodeLoginServiceImpl implements IFLoginService {

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    IClientService clientService;

    @Autowired
    HttpSession session;

    @Autowired
    LoginMessage loginMessage;

    @Autowired
    WxHTTPInterface wxHTTPInterface;

    @Resource(name="wx-app")
    IWxInfo appInfo;

    @Autowired
    WxApi wxApi;

    @Transactional
    @Override
    public ResultInfo login(String loginParams) {

        JSONObject result = new JSONObject();

        /* 取得登录所需的参数 jscode ，转换出现问题时抛出异常，登录逻辑停止。*/
        JSONObject param = null;
        try {
            param = JSONObject.parseObject(loginParams);
        } catch (JSONException exception) {
            throw new SystemException(loginMessage.getE01());
        }

        String code = param.getString(Constant.WX.PARAM.CODE);
        String fromid = param.getString("fromid");

        /* 取得openId */
        String openId = wxApi.getOpenId(
                code,
                appInfo.getAppId(),
                appInfo.getAppSecret());

        /* 取得电话号 */
//        String phoneCode = param.getString(Constant.WX.PARAM.PHONE_CODE);
//        String phone = wxApi.getPhoneNumber(
//                openId,
//                appInfo.getAppId(),
//                appInfo.getAppSecret(),
//                phoneCode);

        QueryWrapper<Client> clientQueryWrapper = new QueryWrapper<Client>();
        clientQueryWrapper.eq("openid", openId);
        List<Client> clients = clientService.list(clientQueryWrapper);
        if (0 == clients.size()) {
            Client client = new Client();
            client.setOpenid(openId);
            client.setClientid(StringGenerator.getID("CLI"));
            client.setHeadimg("https://ymq-1328210466.cos.ap-nanjing.myqcloud.com/static%2Faceping%2Fren.png");
            clientService.save(client);



            result.put("client", client);
            if (StringUtils.hasLength(fromid) && fromid.startsWith("CLI")) {
                Client fromClient = clientService.getById(fromid);


            }

        } else if (1 == clients.size()) {

        } else {
            throw new SystemException("当前账户非法");
        }

        session.setAttribute(Constant.RESULT.CURRENT_CLIENT, result);

        return ResultInfo.success(result);
    }
}
